package com.anuo.app.modules.feed.controller;

import com.anuo.app.common.entity.Request;
import com.anuo.app.common.globalhandle.BaseController;
import com.anuo.app.common.persistence.PageData;
import com.anuo.app.common.utils.Assert;
import com.anuo.app.common.utils.JsonUtil;
import com.anuo.app.common.utils.StringUtil;
import com.anuo.app.modules.feed.entity.*;
import com.anuo.app.modules.feed.service.FeedService;
import com.anuo.app.modules.system.entity.Fileinfo;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class FeedController extends BaseController {
    @Autowired
    FeedService feedService;

    /**
     * 发表动态
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiSaveFeed", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(nickname = "发表动态", value = "发表动态", notes = "发表动态,可以发布图片和视频")
    public String apiSaveFeed(
           @RequestBody String encryptedReq) {
        Feed feed = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<Feed>>() {
        }).getFirstEntity();
        if (StringUtil.isEmpty(feed.getContent()) && feed.getFileinfoList().size() == 0) {
            return rspError("不允许文字和图片都为空, 请填写其中一个");
        }
        feedService.saveFeed(feed);
        return rspSuccess();
    }

    /**
     * 删除动态
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiDeleteFeed", produces = {"application/json;charset=UTF-8"})
    public String apiDeleteFeed(@RequestBody String encryptedReq) {
        Feed feed = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<Feed>>() {
        }).getFirstEntity();
        Assert.isEmpty(feed.getId(), "动态id不能为空");
        feedService.deleteFeed(feed);
        return rspSuccess();
    }


    /**
     * 保存评论
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiSaveFeedComment", produces = {"application/json;charset=UTF-8"})
    public String apiSaveFeedComment(@RequestBody String encryptedReq) {
        FeedComment feedComment = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedComment>>() {
        }).getFirstEntity();
        Assert.isEmpty(feedComment.getFeedid(), "动态id不能为空");
        Assert.isEmpty(feedComment.getUserid(), "用户id不能为空");
        Assert.isEmpty(feedComment.getContent(), "评价的内容不能为空");
        feedService.saveFeedCommnet(feedComment);
        return rspSuccess();
    }

    /**
     * 删除评论
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiDeleteFeedComment", produces = {"application/json;charset=UTF-8"})
    public String apiDeleteFeedComment(@RequestBody String encryptedReq) {
        FeedComment feedComment = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedComment>>() {
        }).getFirstEntity();
        Assert.isEmpty(feedComment.getId(), "评价id不能为空");
        feedService.deleteFeedComment(feedComment);
        return rspSuccess();
    }


    /**
     * 保存动态点赞
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiSaveFeedSupport", produces = {"application/json;charset=UTF-8"})
    public String apiSaveFeedSupport(@RequestBody String encryptedReq) {
        FeedSupport feedSupport = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedSupport>>() {
        }).getFirstEntity();
        Assert.isEmpty(feedSupport.getFeedid(), "动态id不能为空");
        Assert.isEmpty(feedSupport.getUserid(), "用户id不能为空");
        feedService.saveFeedSupport(feedSupport);
        return rspSuccess();
    }

    /**
     * 取消动态点赞
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiDeleteFeedSupport", produces = {"application/json;charset=UTF-8"})
    public String apiDeleteFeedSupport(@RequestBody String encryptedReq) {
        FeedSupport feedSupport = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedSupport>>() {
        }).getFirstEntity();
        Assert.isEmpty(feedSupport.getFeedid(), "动态id不能为空");
        Assert.isEmpty(feedSupport.getUserid(), "用户id不能为空");
        feedService.deleteFeedSupport(feedSupport);
        return rspSuccess();
    }


    /**
     * 保存评论点赞
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiSaveFeedCommentSupport", produces = {"application/json;charset=UTF-8"})
    public String apiSaveFeedCommentSupport(@RequestBody String encryptedReq) {
        FeedCommentSupport feedCommentSupport = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedCommentSupport>>() {
        }).getFirstEntity();
        Assert.isEmpty(feedCommentSupport.getFeed_commentid(), "评论id不能为空");
        Assert.isEmpty(feedCommentSupport.getUserid(), "用户id不能为空");
        feedService.saveFeedCommentSupport(feedCommentSupport);
        return rspSuccess();
    }

    /**
     * 取消评论点赞
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiDeleteFeedCommentSupport", produces = {"application/json;charset=UTF-8"})
    public String apiDeleteFeedCommentSupport(@RequestBody String encryptedReq) {
        FeedCommentSupport feedCommentSupport = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedCommentSupport>>() {
        }).getFirstEntity();
        Assert.isEmpty(feedCommentSupport.getFeed_commentid(), "评论id不能为空");
        Assert.isEmpty(feedCommentSupport.getUserid(), "用户id不能为空");
        feedService.deleteFeedCommentSupport(feedCommentSupport);
        return rspSuccess();
    }


    /**
     * 获取热门动态
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiGetHotFeedList", produces = {"application/json;charset=UTF-8"})
    public String apiGetHotFeedList(@RequestBody String encryptedReq) {
        FeedFilter feedFilter = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedFilter>>() {
        }).getFirstEntity();
        PageData<Feed> pageData = feedService.GetHotFeedList(feedFilter);
        return rspSuccess(pageData);
    }


    /**
     * 获取评论列表
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiGetFeedCommentList", produces = {"application/json;charset=UTF-8"})
    public String apiGetFeedCommentList(@RequestBody String encryptedReq) {
        FeedFilter feedFilter = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedFilter>>() {
        }).getFirstEntity();
        PageData<FeedComment> pageData = feedService.getFeedCommentList(feedFilter);
        return rspSuccess(pageData);
    }

    /**
     * 获取评论列表
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiGetHotFeedCommentList", produces = {"application/json;charset=UTF-8"})
    public String apiGetHotFeedCommentList(@RequestBody String encryptedReq) {
        FeedFilter feedFilter = JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedFilter>>() {
        }).getFirstEntity();
        PageData<FeedComment> pageData = feedService.getHotFeedCommentList(feedFilter);
        return rspSuccess(pageData);
    }

    /**
     * 保存关注
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiSavePersonFollow", produces = {"application/json;charset=UTF-8"})
    public String apiSavePersonFollow(@RequestBody String encryptedReq) {
        FeedFollow feedFollow= JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedFollow>>() {
        }).getFirstEntity();
        feedService.savePersonFollow(feedFollow);
        return rspSuccess();
    }

    /**
     * 获取关注的人动态列表
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiGetFollowedPersonFeedList", produces = {"application/json;charset=UTF-8"})
    public String apiGetFollowedPersonFeedList(@RequestBody String encryptedReq) {
        FeedFilter feedFilter= JsonUtil.fromJson(encryptedReq, new TypeReference<Request<FeedFilter>>() {
        }).getFirstEntity();
        PageData<Feed> pageData= feedService.getFollowedPersonFeedList(feedFilter);
        return rspSuccess(pageData);
    }

}
